﻿//Write a program that compares two char arrays lexicographically (letter by letter).


namespace ComparesCharArrays
{
    using System;

    public class ComparesCharArrays
    {
        public static void Main(string[] args)
        {           
            Console.Write("Enter first array lenght n = ");
            int firstArrayLenght = int.Parse(Console.ReadLine());
            char[] firstArray = new char[firstArrayLenght];

            Console.Write("Enter second array lenght m = ");
            int secondArrayLenght = int.Parse(Console.ReadLine());
            char[] secondArray = new char[secondArrayLenght];

            Console.WriteLine("Enter {0} elements of the first array", firstArrayLenght);
            PopulateElementsInArrays(firstArrayLenght, firstArray);
            Console.WriteLine("Enter {0} elements of the second array", secondArrayLenght);
            PopulateElementsInArrays(secondArrayLenght, secondArray);
            
            Console.WriteLine("\nResult compared lexicography:");
            int lenght = Math.Min(firstArrayLenght, secondArrayLenght);
            for (int i = 0; i < lenght; i++)
            {
                if (firstArray[i] < secondArray[i])
                {
                    Console.WriteLine("{0} < {1}", firstArray[i], secondArray[i]);
                }
                else if (firstArray[i] > secondArray[i])
                {
                    Console.WriteLine("{0} > {1}", firstArray[i], secondArray[i]);
                }
                else
                {
                    Console.WriteLine("{0} = {1}", firstArray[i], secondArray[i]);
                }
            }        
        }      

        private static void PopulateElementsInArrays(int arraysLenght, char[] arrays)
        {
            string userString = Console.ReadLine();
            for (int arrayIndex = 0, charOfStringIndex = 0; arrayIndex < arraysLenght; arrayIndex++, charOfStringIndex++)
            {
                if (charOfStringIndex < userString.Length)
                {
                    arrays[arrayIndex] = userString[charOfStringIndex];
                }
                else
                {
                    Console.WriteLine("Enter more characters!");
                    userString = Console.ReadLine();
                    charOfStringIndex = -1;
                    arrayIndex--;
                }
            }
        }
    }
}